//引入组件库
var nf = sm("do_Notification");
var root = ui("$");
var do_App = sm("do_App");
var do_Page = sm("do_Page");
var hashdata = mm("do_HashData");
var userUrl = require("url/user");
var qqHttp = require("helper/http");
var helperOpen = require("helper/open");
var imgTool = require("helper/picture");
// 实例化一个BaiduLocation对象
var dingwei = sm("do_BaiduLocation");

// UI
var headerAout = ui("headerAout");

// 本页面-ajax业务请求url
var httpUrl = userUrl.url.suggest.add;

// 加载头部
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});
// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");

var headerRight = ui(addheader + ".headerRight");
var labelNext = ui(addheader + ".labelNext");
labelNext.visible = true;

headerLeft.on("touch", function() {
	do_App.closePage()
});
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "反馈",
	"labelNext" : "确定"
})
header.refreshData();

headerRight.on("touch", function() {
	create();
})
var imgMax = 3;
/** **************处理-图片显示UI-end**************** */
var aLayoutImg = ui("aLayoutImg");
var aLayoutImgRoot = ui("aLayoutImgRoot");

var imgGridUI = aLayoutImgRoot.add("imgGridShower",
		"source://view/public/imgselect/imgGrid.ui", 20, 15);
var imgGridView = ui(imgGridUI + ".imgGrid");
var listdataImg = mm("do_ListData");
imgGridView.bindItems(listdataImg);
imgGridView.on("touch", function(data, e) {
	imgMax=imgMax+1;
	listdataImg.removeData([ data ]);
	imgGridView.refreshItems();
});
/** **************处理-图片显示UI-end**************** */

/** **************点击-添加图片-begin**************** */
aLayoutImg.on("touch", function(data, e) {
	if (imgMax==0) {
		return;
	}
	imgTool.imgMulti(imgMax, selectImgBack);// 选择多张图片
});

function selectImgBack(imgList) {
	imgMax=imgMax-imgList.length;
	listdataImg.addData(imgList.map(function(path) {
		return {
			"imgPath" : path
		}
	}));
	imgGridView.refreshItems();
}
/** **************点击-添加图片-end**************** */

var layout_main = ui("do_LinearLayout_main");
var addboxContent = layout_main.add("boxContent",
		"source://view/reuse/form/mutiTextarea.ui", 0, 0);
var boxContent = ui("boxContent");
hashdata.addData({
	"textAreaHint" : '反馈内容'
});
boxContent.bindData(hashdata);
boxContent.refreshData();
var textBoxContent = ui(addboxContent + ".textArea");

function create(){
	do_Page.hideKeyboard();
	if (textBoxContent.text.trim() == '' || textBoxContent.text == null) {
		nf.toast("请填写反馈信息");
		return;
	}
	
	var imgFiles = [];
	for (var i = 0; i < listdataImg.getCount(); i++) {
		imgFiles.push(listdataImg.getOne(i).imgPath);
	}
	if (listdataImg.getCount() > 0) {
		qqHttp.form(imgFiles, "user/suggest", function(imgDatas) {
			var string4imgs = imgDatas.path;// 返回图片云端路径（后部份）用分号隔开
			var params = {
				"img" : string4imgs,
				"content" : textBoxContent.text,
			};
			qqHttp.post(httpUrl, params, function(data) {
				nf.toast("反馈成功，请耐心等待工作人员的答复。");
				do_App.closePage({fromPage:'user_suggest_add'});
			})
		});
	} else {
		var params = {
			"img" :"",
			"content" : textBoxContent.text,
		};
		qqHttp.post(httpUrl, params, function(data) {
			nf.toast("反馈成功，请耐心等待工作人员的答复。");
			do_App.closePage({fromPage:'user_suggest_add'});
		})
	}
}

// 点击背景隐藏输入键盘
root.on("touch", function() {
	do_Page.hideKeyboard();
})